package com.example.demo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/sayHi")
    public String sayHi(){
        System.out.println("执行:sayHi()方法");
        return "Hi World";
    }

    @RequestMapping("/sayHello")
    public String sayHello(){
        System.out.println("执行:sayHello()方法");
        int n = 1 / 0;
        return "Hello World";
    }

    @RequestMapping("/login")
    public boolean login(HttpServletRequest request, String username, String password){
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)){
            if ("admin".equals(username) && "admin".equals(password)){
                HttpSession session = request.getSession();
                session.setAttribute("userInfo", "userInfo");
                return true;
            }
        }
        return false;
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, String username, String password){
        int num = 10 / 0;
        return "Hello Index";
    }
}
